package com.xique.park.service.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xique.common.core.domain.R;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.park.bean.domain.ParkDeviceSub;
import com.xique.park.bean.request.web.subDev.ParkSubDevListRequest;
import com.xique.park.bean.transform.web.subDev.ParkSubDevTransform;
import com.xique.park.bean.vo.web.subDev.ParkSubDevVo;
import com.xique.park.service.biz.service.IParkSubDevBizService;
import com.xique.park.service.service.IParkDeviceSubService;
import com.xique.system.api.RemoteSystemUserService;
import com.xique.system.api.dto.OperateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liuxueli
 */
@Service
public class ParkSubDevBizServiceImpl implements IParkSubDevBizService {

    @Autowired
    private IParkDeviceSubService parkDeviceSubService;

    @Autowired
    private RemoteSystemUserService remoteSystemUserService;

    @Override
    public AjaxResult getParkSubDevList(ParkSubDevListRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        IPage<ParkDeviceSub> page = parkDeviceSubService.selectParkSubDevPage(request);
        List<ParkDeviceSub> list = page.getRecords();

        List<ParkSubDevVo> vos = list.stream().map(parkDeviceSub -> {
            ParkSubDevVo parkSubDevVo = ParkSubDevTransform.transformParkSubDevVo(parkDeviceSub);
            Long updateUserId = parkDeviceSub.getUpdateUser();
            if (updateUserId != null){
                R<OperateDTO> result = remoteSystemUserService.getOperate(updateUserId);
                if (result.getData() != null){
                    parkSubDevVo.setUpdateBy(result.getData().getOperateName());
                }
            }
            return parkSubDevVo;
        }).collect(Collectors.toList());


        Map<String, Object> map = new HashMap<>(2);
        map.put("list", vos);
        map.put("total", page.getTotal());
        return AjaxResult.success(map);
    }

}
